<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: RunsTp <RunsTP@163.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\User;
use think\facade\View;
use think\Response;
/**
 *
 */
class ProfileController extends Base
{

    //public 绝对路径， 用于用户提交相对路径时追加
    private $public_path;
    //相对路径，用于返回前端
    private $head_return_path;
    //绝对路径，用于存储地址
    private $head_save_path;

    public function initialize()
    {
        $this->public_path = app()->getRootPath().'public';
        $this->head_return_path = 'upload/head';
        $this->head_save_path = app()->getRootPath().'public/upload/head';

        return parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 修改个人信息
     */
    public function index(User $user)
    {

        //提交修改
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            if (empty($param)) {
                return json(msg(-1, '', 'not found user'));
            }

            if ($param['new_password'] !== $param['re_new_password']) {
                return json(msg(-2, '', '两次输入的密码不相同'));
            }

            $user_data = $user::find((session('id')));

            if (is_null($user_data)) {
                return json(msg(-1, '', 'not found user'));
            }

            if ($user_data->password !== sha1($param['old_password'] . $user_data->pass_salt)) {
                return json(msg(-3, '', '原始密码错误'));
            }

            if ($user_data->password !== sha1($param['old_password'] . $user_data->pass_salt)) {
                return json(msg(-4, '', '新密码不能和旧密码相同'));
            }
            $user_data->password = $param['new_password'];
            $res = $user_data->save();

            return json(msg(1, '', '修改成功'));
        }

        return View::fetch();
    }

}
